package com.erp.erp_servers.Bpmn.imp;

import com.erp.erp_servers.Bpmn.IBpmnService;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
import org.activiti.engine.impl.pvm.process.TransitionImpl;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ImpBpmnService
 * @Description TODO
 * @Author songjizhen
 * @Date 2020-07-26 12:47
 * @Version 1.0
 */

public class ImpBpmnService implements IBpmnService {
    @Autowired
    RepositoryService repositoryService;

    @Autowired
    RuntimeService runtimeService;

    @Autowired
    TaskService taskService;
    @Autowired
    ProcessEngine processEngine;
    @Autowired
    HistoryService historyService;

    /**
     * 流程部署
     *
     * @param bpmname    流程设计文件名（*.bpmn）
     * @param delpoyname 部署名称
    背后影响的表:
     *      act_re_deployment: 部署信息
     *      act_re_procdef: 流程定义
     *      act_ge_bytearray: 流程定义的bpmn文件和png文件
     */
    @Override
    public Deployment deployProcess(String bpmname, String delpoyname) {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        DeploymentBuilder builder = repositoryService.createDeployment();
        builder.addClasspathResource("bpmn/"+bpmname+".bpmn");//bpmn文件的名称
        if(delpoyname !=null) builder.name(delpoyname);
        Deployment deploy = builder.deploy();
        return deploy;
    }

    /**
     * 查询流程定义
     *
     * @param deploymentId              使用部署对象ID查询
     * @param processDefinitionId       使用流程定义ID查询
     * @param processDefinitionKey      使用流程定义的key查询
     * @param processDefinitionNameLike 使用流程定义的名称模糊查询
     * @param count                     返回结果集数量
     *                                  以上5个参数为互斥参数，只能有
     *                                  以下两个参数用用分页，可与processDefinitionNameLike联合使用
     * @param firstResult               开始条数
     * @param maxResults                截止条数
     * @return
     */
    @Override
    public Object findProcessDefinition(String deploymentId, String processDefinitionId, String processDefinitionKey, String processDefinitionNameLike, Boolean count, int firstResult, int maxResults) {
        if(count){//返回结果集数量
            long count1 = processEngine.getRepositoryService()//与流程定义和部署相关的service
                    .createProcessDefinitionQuery()//创建一个流程定义的查询
                    .count();
            //此处进行返回
            return count1;
        }
        List<ProcessDefinition> list = this.getProcessDefinition(deploymentId,processDefinitionId,processDefinitionKey,
                processDefinitionNameLike,count,firstResult,maxResults);
        return list;
    }

    /**
     * 根据流程部署ID，删除流程定义
     *
     * @param deploymentId
     */
    @Override
    public Boolean deleteProcessDefinition(String deploymentId) {
        /**
         * 级联删除
         *       不管流程是否启动，都能可以删除
         */
        processEngine.getRepositoryService()//
                .deleteDeployment(deploymentId, true);
        return true;
    }

    /**
     * 查看流程图
     *
     * @param deploymentId
     * @param response
     */
    @Override
    public Boolean viewPic(String deploymentId, HttpServletResponse response) throws IOException {
          //获取图片资源名称
        List<String> list = processEngine.getRepositoryService()//
                .getDeploymentResourceNames(deploymentId);
        //定义图片资源的名称
        String resourceName = "";
        if (list != null && list.size() > 0) {
            for (String name : list) {
                if (name.indexOf(".png") >= 0) {
                    resourceName = name;
                }
            }
        }

        //获取图片的输入流
        InputStream in = processEngine.getRepositoryService()//
                .getResourceAsStream(deploymentId, resourceName);

        OutputStream out = response.getOutputStream();
        byte[] buf = new byte[2048];
        int length = 0;
        while (-1 != (length = in.read(buf, 0, buf.length))) {
            out.write(buf );
            System.out.println(buf);
        }
        out.flush();
        out.close();
        in.close();
       return true;
    }

    /**
     * 根据流程定义的key（即流程图中的id）启动流程
     *
     * @param processDefinitionKey
    背后影响的表:
     *      act_hi_actinst: 已完成的活动信息
     *      act_hi_identitylink: 参与者信息
     *      act_hi_procinst: 流程实例
     *      act_hi_taskinst: 任务实例
     *      act_ru_execution: 执行表
     *      act_ru_identitylink: 参与者信息
     *      act_ru_task: 任务
     * @return
     */
    @Override
    public ProcessInstance startProcessInstance(String processDefinitionKey,Map<String, Object>map) {
        // startProcessInstanceByKey方法还可以设置其他的参数，比如流程变量。
        //使用流程定义的key启动流程实例，key对应helloworld.bpmn文件中id的属性值，使用key值启动，默认是按照最新版本的流程定义启动
        ProcessInstance pi = processEngine.getRuntimeService()
                .startProcessInstanceByKey(processDefinitionKey,map);
        return pi;
    }

    /**
     * 查询个人待办事项
     *
     * @param userCode
     * @return
     */
    @Override
    public List<Task> findPersonalTask(String userCode) {
        List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
                                                    .createTaskQuery()//创建任务查询对象
                                                    .taskAssignee(userCode)////指定个人任务查询，指定办理人
                                                    .orderByTaskCreateTime().asc()//使用创建时间的升序排列
                                                    .list();
        return list;

       /* List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
                .createTaskQuery()//创建任务查询对象
                *//**查询条件（where部分）*//*
                .taskAssignee(userCode)//指定个人任务查询，指定办理人
//                        .taskCandidateUser(candidateUser)//组任务的办理人查询
//                        .processDefinitionId(processDefinitionId)//使用流程定义ID查询
//                        .processInstanceId(processInstanceId)//使用流程实例ID查询
//                        .executionId(executionId)//使用执行对象ID查询
                *//**排序*//*
                .orderByTaskCreateTime().asc()//使用创建时间的升序排列
                *//**返回结果集*//*
//                        .singleResult()//返回惟一结果集
//                        .count()//返回结果集的数量
//                        .listPage(firstResult, maxResults);//分页查询
                .list();//返回列表*/

    }

    /**
     * 查询已办
     *
     * @param userCode
     * @return
     */
    @Override
    public List<HistoricTaskInstance> findHistoryTash(String userCode, int firstResult, int maxResults) {
        List<HistoricTaskInstance> taskList  = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(userCode)
                .finished().orderByTaskCreateTime().desc().listPage(firstResult,maxResults);

        return taskList;
    }

    /**
     * 任务处理
     *
     * @param taskId
     */
    @Override
    public Boolean completePersonalTask(String taskId,Map<String, Object> variables, String activityId) throws Exception {
        if (variables == null) {
            variables = new HashMap<String, Object>();
        }
        // 跳转节点为空，默认提交操作
        if (StringUtils.isEmpty(activityId)) {
            taskService.complete(taskId, variables);
            return true;
        } else {// 流程转向操作,即退回
            this.turnTransition(taskId, activityId, variables);
            return true;
        }

    }
    /**
     * 流程转向操作
     *
     * @param taskId
     *            当前任务ID
     * @param activityId
     *            目标节点任务ID
     * @param variables
     *            流程变量
     * @throws Exception
     */
    private  void turnTransition(String taskId, String activityId, Map<String, Object> variables) throws Exception {
        // 当前节点
        ActivityImpl currActivity = findActivitiImpl(taskId, null);
        // 清空当前流向
        List<PvmTransition> oriPvmTransitionList = clearTransition(currActivity);

        // 创建新流向
        TransitionImpl newTransition = currActivity.createOutgoingTransition();
        // 目标节点
        ActivityImpl pointActivity = findActivitiImpl(taskId, activityId);
        // 设置新流向的目标节点
        newTransition.setDestination(pointActivity);

        // 执行转向任务
        taskService.complete(taskId, variables);
        // 删除目标节点新流入
        pointActivity.getIncomingTransitions().remove(newTransition);

        // 还原以前流向
        restoreTransition(currActivity, oriPvmTransitionList);
    }
    /**
     * 还原指定活动节点流向
     *
     * @param activityImpl
     *            活动节点
     * @param oriPvmTransitionList
     *            原有节点流向集合
     */
    private  void restoreTransition(ActivityImpl activityImpl,
                                          List<PvmTransition> oriPvmTransitionList) {
        // 清空现有流向
        List<PvmTransition> pvmTransitionList = activityImpl
                .getOutgoingTransitions();
        pvmTransitionList.clear();
        // 还原以前流向
        for (PvmTransition pvmTransition : oriPvmTransitionList) {
            pvmTransitionList.add(pvmTransition);
        }
    }
    /**
     * 清空指定活动节点流向
     *
     * @param activityImpl
     *            活动节点
     * @return 节点流向集合
     */
    private  List<PvmTransition> clearTransition(ActivityImpl activityImpl) {
        // 存储当前节点所有流向临时变量
        List<PvmTransition> oriPvmTransitionList = new ArrayList<PvmTransition>();
        // 获取当前节点所有流向，存储到临时变量，然后清空
        List<PvmTransition> pvmTransitionList = activityImpl
                .getOutgoingTransitions();
        for (PvmTransition pvmTransition : pvmTransitionList) {
            oriPvmTransitionList.add(pvmTransition);
        }
        pvmTransitionList.clear();

        return oriPvmTransitionList;
    }
    /**
     * 根据任务ID和节点ID获取活动节点 <br>
     *
     * @param taskId
     *            任务ID
     * @param activityId
     *            活动节点ID <br>
     *            如果为null或""，则默认查询当前活动节点 <br>
     *            如果为"end"，则查询结束节点 <br>
     *
     * @return
     * @throws Exception
     */
    private  ActivityImpl findActivitiImpl(String taskId, String activityId)
            throws Exception {
        // 取得流程定义
        ProcessDefinitionEntity processDefinition = findProcessDefinitionEntityByTaskId(taskId);

        // 获取当前活动节点ID
        if (StringUtils.isEmpty(activityId)) {
            activityId = findTaskById(taskId).getTaskDefinitionKey();
        }

        // 根据流程定义，获取该流程实例的结束节点
        if (activityId.toUpperCase().equals("END")) {
            for (ActivityImpl activityImpl : processDefinition.getActivities()) {
                List<PvmTransition> pvmTransitionList = activityImpl
                        .getOutgoingTransitions();
                if (pvmTransitionList.isEmpty()) {
                    return activityImpl;
                }
            }
        }

        // 根据节点ID，获取对应的活动节点
        ActivityImpl activityImpl = ((ProcessDefinitionImpl) processDefinition)
                .findActivity(activityId);

        return activityImpl;
    }
    /**
     * 根据任务ID获得任务实例
     *
     * @param taskId
     *            任务ID
     * @return
     * @throws Exception
     */
    private  TaskEntity findTaskById(String taskId) throws Exception {
        TaskEntity task = (TaskEntity) taskService.createTaskQuery().taskId(
                taskId).singleResult();
        if (task == null) {
            throw new Exception("任务实例未找到!");
        }
        return task;
    }
    /**
     * 根据任务ID获取流程定义
     *
     * @param taskId
     *            任务ID
     * @return
     * @throws Exception
     */
    public  ProcessDefinitionEntity findProcessDefinitionEntityByTaskId(
            String taskId) throws Exception {
        // 取得流程定义
        ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
                .getDeployedProcessDefinition(findTaskById(taskId)
                        .getProcessDefinitionId());

        if (processDefinition == null) {
            throw new Exception("流程定义未找到!");
        }

        return processDefinition;
    }
    /**
     * 根据流程实例id查询流程状态,判断流程走到哪一个节点
     *
     * @param processInstanceId
     * @return
     */
    @Override
    public ProcessInstance isProcessActive(String processInstanceId) {
        ProcessInstance pi = processEngine.getRuntimeService()//表示正在执行的流程实例和执行对象
                .createProcessInstanceQuery()//创建流程实例查询
                .processInstanceId(processInstanceId)//使用流程实例ID查询
                .singleResult();
        return pi;
    }

    /**
     * 根据流程实例id查询历史信息
     *
     * @param processInstanceId
     * @return
     */
    @Override
    public List<HistoricTaskInstance> findHistoryTaskByPID(String processInstanceId) {
        List<HistoricTaskInstance> list = processEngine.getHistoryService()// 与历史数据（历史表）相关的Service
                .createHistoricTaskInstanceQuery()// 创建历史任务实例查询
                .processInstanceId(processInstanceId)//使用流程实例ID查询
                .orderByHistoricTaskInstanceEndTime().desc().list();
        return list;
    }

    /**
     * 中止流程
     *
     * @param taskId
     * @return
     */
    @Override
    public Boolean endProcess(String taskId) throws Exception {
        ActivityImpl endActivity = findActivitiImpl(taskId, "end");
        completePersonalTask(taskId, null, endActivity.getId());
        return true;
    }

    /**
     * 根据当前任务ID，查询可以驳回的任务节点
     *
     * @param taskId
     * @return
     */
    @Override
    public List<ActivityImpl> findBackAvtivity(String taskId) throws Exception {
        List<ActivityImpl> rtnList =  iteratorBackActivity(taskId, findActivitiImpl(taskId,
                null), new ArrayList<ActivityImpl>(),
                new ArrayList<ActivityImpl>());
       return reverList(rtnList);

    }
    /**
     * 反向排序list集合，便于驳回节点按顺序显示
     *
     * @param list
     * @return
     */
    private  List<ActivityImpl> reverList(List<ActivityImpl> list) {
        List<ActivityImpl> rtnList = new ArrayList<ActivityImpl>();
        // 由于迭代出现重复数据，排除重复
        for (int i = list.size(); i > 0; i--) {
            if (!rtnList.contains(list.get(i - 1)))
                rtnList.add(list.get(i - 1));
        }
        return rtnList;
    }

    /**
     * 迭代循环流程树结构，查询当前节点可驳回的任务节点
     *
     * @param taskId
     *            当前任务ID
     * @param currActivity
     *            当前活动节点
     * @param rtnList
     *            存储回退节点集合
     * @param tempList
     *            临时存储节点集合（存储一次迭代过程中的同级userTask节点）
     * @return 回退节点集合
     */
    private  List<ActivityImpl> iteratorBackActivity(String taskId,
                       ActivityImpl currActivity, List<ActivityImpl> rtnList,
                       List<ActivityImpl> tempList) throws Exception {
        // 查询流程定义，生成流程树结构
        ProcessInstance processInstance = findProcessInstanceByTaskId(taskId);

        // 当前节点的流入来源
        List<PvmTransition> incomingTransitions = currActivity
                .getIncomingTransitions();
        // 条件分支节点集合，userTask节点遍历完毕，迭代遍历此集合，查询条件分支对应的userTask节点
        List<ActivityImpl> exclusiveGateways = new ArrayList<ActivityImpl>();
        // 并行节点集合，userTask节点遍历完毕，迭代遍历此集合，查询并行节点对应的userTask节点
        List<ActivityImpl> parallelGateways = new ArrayList<ActivityImpl>();
        // 遍历当前节点所有流入路径
        for (PvmTransition pvmTransition : incomingTransitions) {
            TransitionImpl transitionImpl = (TransitionImpl) pvmTransition;
            ActivityImpl activityImpl = transitionImpl.getSource();
            String type = (String) activityImpl.getProperty("type");
            /**
             * 并行节点配置要求：<br>
             * 必须成对出现，且要求分别配置节点ID为:XXX_start(开始)，XXX_end(结束)
             */
            if ("parallelGateway".equals(type)) {// 并行路线
                String gatewayId = activityImpl.getId();
                String gatewayType = gatewayId.substring(gatewayId
                        .lastIndexOf("_") + 1);
                if ("START".equals(gatewayType.toUpperCase())) {// 并行起点，停止递归
                    return rtnList;
                } else {// 并行终点，临时存储此节点，本次循环结束，迭代集合，查询对应的userTask节点
                    parallelGateways.add(activityImpl);
                }
            } else if ("startEvent".equals(type)) {// 开始节点，停止递归
                return rtnList;
            } else if ("userTask".equals(type)) {// 用户任务
                tempList.add(activityImpl);
            } else if ("exclusiveGateway".equals(type)) {// 分支路线，临时存储此节点，本次循环结束，迭代集合，查询对应的userTask节点
                currActivity = transitionImpl.getSource();
                exclusiveGateways.add(currActivity);
            }
        }
        /**
         * 迭代条件分支集合，查询对应的userTask节点
         */
        for (ActivityImpl activityImpl : exclusiveGateways) {
            iteratorBackActivity(taskId, activityImpl, rtnList, tempList);
        }

        /**
         * 迭代并行集合，查询对应的userTask节点
         */
        for (ActivityImpl activityImpl : parallelGateways) {
            iteratorBackActivity(taskId, activityImpl, rtnList, tempList);
        }

        /**
         * 根据同级userTask集合，过滤最近发生的节点
         */
        currActivity = filterNewestActivity(processInstance, tempList);
        if (currActivity != null) {
            // 查询当前节点的流向是否为并行终点，并获取并行起点ID
            String id = findParallelGatewayId(currActivity);
            if (StringUtils.isEmpty(id)) {// 并行起点ID为空，此节点流向不是并行终点，符合驳回条件，存储此节点
                rtnList.add(currActivity);
            } else {// 根据并行起点ID查询当前节点，然后迭代查询其对应的userTask任务节点
                currActivity = findActivitiImpl(taskId, id);
            }

            // 清空本次迭代临时集合
            tempList.clear();
            // 执行下次迭代
            iteratorBackActivity(taskId, currActivity, rtnList, tempList);
        }
        return rtnList;
    }
    /**
     * 根据当前节点，查询输出流向是否为并行终点，如果为并行终点，则拼装对应的并行起点ID
     *
     * @param activityImpl
     *            当前节点
     * @return
     */
    private  String findParallelGatewayId(ActivityImpl activityImpl) {
        List<PvmTransition> incomingTransitions = activityImpl
                .getOutgoingTransitions();
        for (PvmTransition pvmTransition : incomingTransitions) {
            TransitionImpl transitionImpl = (TransitionImpl) pvmTransition;
            activityImpl = transitionImpl.getDestination();
            String type = (String) activityImpl.getProperty("type");
            if ("parallelGateway".equals(type)) {// 并行路线
                String gatewayId = activityImpl.getId();
                String gatewayType = gatewayId.substring(gatewayId
                        .lastIndexOf("_") + 1);
                if ("END".equals(gatewayType.toUpperCase())) {
                    return gatewayId.substring(0, gatewayId.lastIndexOf("_"))
                            + "_start";
                }
            }
        }
        return null;
    }
    /**
     * 根据流入任务集合，查询最近一次的流入任务节点
     *
     * @param processInstance
     *            流程实例
     * @param tempList
     *            流入任务集合
     * @return
     */
    private  ActivityImpl filterNewestActivity(ProcessInstance processInstance,
                                                     List<ActivityImpl> tempList) {
        while (tempList.size() > 0) {
            ActivityImpl activity_1 = tempList.get(0);
            HistoricActivityInstance activityInstance_1 = findHistoricUserTask(
                    processInstance, activity_1.getId());
            if (activityInstance_1 == null) {
                tempList.remove(activity_1);
                continue;
            }

            if (tempList.size() > 1) {
                ActivityImpl activity_2 = tempList.get(1);
                HistoricActivityInstance activityInstance_2 = findHistoricUserTask(
                        processInstance, activity_2.getId());
                if (activityInstance_2 == null) {
                    tempList.remove(activity_2);
                    continue;
                }

                if (activityInstance_1.getEndTime().before(
                        activityInstance_2.getEndTime())) {
                    tempList.remove(activity_1);
                } else {
                    tempList.remove(activity_2);
                }
            } else {
                break;
            }
        }
        if (tempList.size() > 0) {
            return tempList.get(0);
        }
        return null;
    }
    /**
     * 查询指定任务节点的最新记录
     *
     * @param processInstance
     *            流程实例
     * @param activityId
     * @return
     */
    private  HistoricActivityInstance findHistoricUserTask(
            ProcessInstance processInstance, String activityId) {
        HistoricActivityInstance rtnVal = null;
        // 查询当前流程实例审批结束的历史节点
        List<HistoricActivityInstance> historicActivityInstances = historyService
                .createHistoricActivityInstanceQuery().activityType("userTask")
                .processInstanceId(processInstance.getId()).activityId(
                        activityId).finished()
                .orderByHistoricActivityInstanceEndTime().desc().list();
        if (historicActivityInstances.size() > 0) {
            rtnVal = historicActivityInstances.get(0);
        }

        return rtnVal;
    }

    /**
     * 根据任务ID获取对应的流程实例
     *
     * @param taskId
     *            任务ID
     * @return
     * @throws Exception
     */
    public  ProcessInstance findProcessInstanceByTaskId(String taskId)
            throws Exception {
        // 找到流程实例
        ProcessInstance processInstance = runtimeService
                .createProcessInstanceQuery().processInstanceId(
                        findTaskById(taskId).getProcessInstanceId())
                .singleResult();
        if (processInstance == null) {
            throw new Exception("流程实例未找到!");
        }
        return processInstance;
    }
    private List<ProcessDefinition> getProcessDefinition(String deploymentId,String processDefinitionId,String processDefinitionKey,String processDefinitionNameLike
            ,Boolean count,int firstResult, int maxResults){
        ProcessDefinitionQuery processDefinitionQuery = processEngine.getRepositoryService()//与流程定义和部署相关的service
                .createProcessDefinitionQuery();//创建一个流程定义的查询
        if(deploymentId !=null){
            //使用部署对象ID查询
            List<ProcessDefinition> list = processDefinitionQuery.deploymentId(deploymentId).list();
            return list;
        }
        if(processDefinitionId !=null){
            //使用流程定义ID查询
            List<ProcessDefinition> list = processDefinitionQuery.processDefinitionId(processDefinitionId).list();
            return list;
        }
        if(processDefinitionKey !=null){
            ////使用流程定义的key查询
            List<ProcessDefinition> list = processDefinitionQuery.processDefinitionKey(processDefinitionKey).list();
            return list;
        }
        if(processDefinitionNameLike !=null){
            ////使用流程定义的名称模糊查询
            if(firstResult >0){
                List<ProcessDefinition> list = processDefinitionQuery.processDefinitionNameLike(processDefinitionNameLike)
                        .orderByProcessDefinitionName().desc()
                        .orderByProcessDefinitionVersion().desc()
                        .listPage(firstResult, maxResults);
                return list;
            }
            List<ProcessDefinition> list = processDefinitionQuery.processDefinitionNameLike(processDefinitionNameLike)
                    .orderByProcessDefinitionName().desc()
                    .orderByProcessDefinitionVersion().desc()
                    .list();
            return list;
        }
        if(firstResult >0){
            List<ProcessDefinition> list = processDefinitionQuery
                    .orderByProcessDefinitionName().desc()
                    .orderByProcessDefinitionVersion().desc()
                    .listPage(firstResult, maxResults);
            return list;
        }
               /* .orderByProcessDefinitionVersion().asc()//按照版本的升序排列
                .list();*///返回一个集合列表，封装流程定义
        // List<ProcessDefinition> list= processEngine.getRepositoryService()//与流程定义和部署相关的service
        //                            .createProcessDefinitionQuery()//创建一个流程定义的查询
        /**指定查询条件,where条件*/
        //                        .deploymentId(deploymentId)//使用部署对象ID查询
        //                        .processDefinitionId(processDefinitionId)//使用流程定义ID查询
        //                        .processDefinitionKey(processDefinitionKey)//使用流程定义的key查询
        //                        .processDefinitionNameLike(processDefinitionNameLike)//使用流程定义的名称模糊查询

        /**排序*/
        //                    .orderByProcessDefinitionVersion().asc()//按照版本的升序排列
        //                        .orderByProcessDefinitionName().desc()//按照流程定义的名称降序排列

        /**返回的结果集*/
        //                   .list();//返回一个集合列表，封装流程定义
        //                        .singleResult();//返回惟一结果集
        //                        .count();//返回结果集数量
        //                        .listPage(firstResult, maxResults);//分页查询
        return  null;
    }
}
